package com.peng.servlet;

import com.peng.entity.work;
import com.peng.service.impl.workServiceImpl;
import com.peng.service.workService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 欧阳欣鹏 on 2018/8/13.
 */
public class WorkAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

        request.setCharacterEncoding("utf-8");
        int id=Integer.parseInt(request.getParameter("id"));
        workService ws=new workServiceImpl();
        int i=ws.findbyIdProduct();
        int a=ws.findbyIdStaff();
        work w=new work();



        if ((Integer.parseInt(request.getParameter("staffid"))<=a)){
            w.setStaffid(Integer.parseInt(request.getParameter("staffid")));
        }else{
           out.print("<script language='JavaScript'>history.go(-1);alert('编号不符合规定')</script>");
           return;
        }
        w.setUserName(request.getParameter("name"));
        w.setUserPhone(request.getParameter("phone"));
        w.setUserAddress(request.getParameter("address"));
        w.setWorkType(1);
        w.setStatus(Integer.parseInt(request.getParameter("status")));
        if ((Integer.parseInt(request.getParameter("product"))<=i)){
            w.setWpid(Integer.parseInt(request.getParameter("product")));
        }else{
            out.print("<script language='JavaScript'>history.go(-1);alert('净水器编号不符合规定')</script>");

            return;
        }
        ws.insert(w);
        if (id==1){
            response.sendRedirect("WorkIndexServlet?status=1&&workType=1");
        }
        if(id==2){
            response.sendRedirect("WorkIndexServlet?status=2&&workType=1");
        }
        if (id==3){
            response.sendRedirect("WorkIndexServlet?status=3&&workType=1");
        }

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }
}
